<?php

namespace backend\models;

use Yii;


/**
 * This is the model class for table "commodity".
 *
 * @property string $name
 * @property string $type
 * @property string $icon
 * @property integer $count
 * @property double $purchase_price
 * @property double $price
 * @property string $desc
 * @property string $details
 * @property integer $id
 */
class Commodity extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'commodity';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'type', 'icon_url', 'count', 'purchase_price', 'sale_price', 'desc', 'details'], 'required'],
            [['count'], 'integer'],
            [['purchase_price', 'sale_price'], 'number'],
            [['icon_url'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',],
            [['name', 'type', 'desc'], 'string', 'max' => 255],
            
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'name' => 'Name',
            'type' => 'Type',
            'icon_url' => 'Icon Url',
            'count' => 'Count',
            'purchase_price' => 'Purchase Price',
            'sale_price' => 'Sale Price',
            'desc' => 'Desc',
            'details' => 'Details',
            // 'detailsUrl' => 'DetailsUrl',
            'id' => 'ID',
        ];
    }

    public function upload()
    {
        foreach ($this->details as $file) {
            $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
        }
        return true;
    }
}
